package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.DouLuoDaLuPatronsBattle;
import com.yanqu.road.entity.activity.douluodalu.DouLuoDaLuPatronsBattleEnemy;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuPatronsBattleData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoDaLuPatronsBattleDataDaoImpl extends TempDataObjectDao<DouLuoDaLuPatronsBattleData> {


    public static String getDouLuoDaLuPatronsBattleDataSql = "select * from `t_c_douluodalu_patrons_battle` where `activityId`=? and groupId=?";
    public Map<Long, DouLuoDaLuPatronsBattleData> getDouLuoDaLuPatronsBattleData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, DouLuoDaLuPatronsBattleData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuPatronsBattleDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoDaLuPatronsBattleData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertDouLuoDaLuPatronsBattleDataSql = "insert into `t_c_douluodalu_patrons_battle`(activityId,userId,serverId,score,beHitTimes,lastEnemyId,resetTime,nextEnemyUserId,captureEnemyUserId,curEnemyUserId," +
            "allBattleTimes,battleType,battleEnd,allWin,randomPatronsList,battleList,enemyBattleList,groupId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuPatronsBattleData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeHitTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastEnemyId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNextEnemyUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCaptureEnemyUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCurEnemyUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAllBattleTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleType()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isBattleEnd()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAllWin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRandomPatronsList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBattleList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyBattleList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuPatronsBattleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuPatronsBattleDataSql = "update `t_c_douluodalu_patrons_battle` set serverId=?,score=?,beHitTimes=?,lastEnemyId=?,resetTime=?,nextEnemyUserId=?,captureEnemyUserId=?,curEnemyUserId=?," +
            "allBattleTimes=?,battleType=?,battleEnd=?,allWin=?,randomPatronsList=?,battleList=?,enemyBattleList=? where activityId=? and userId=? and groupId=?";

    @Override
    protected boolean updateData(DouLuoDaLuPatronsBattleData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeHitTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastEnemyId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNextEnemyUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCaptureEnemyUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCurEnemyUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAllBattleTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleType()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isBattleEnd()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAllWin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRandomPatronsList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBattleList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyBattleList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuPatronsBattleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuPatronsBattleData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuPatronsBattleData battleData = new DouLuoDaLuPatronsBattleData();
        battleData.setActivityId(rs.getInt("activityId"));
        battleData.setUserId(rs.getLong("userId"));
        battleData.setServerId(rs.getLong("serverId"));
        battleData.setScore(rs.getLong("score"));
        battleData.setBeHitTimes(rs.getInt("beHitTimes"));
        battleData.setLastEnemyId(rs.getLong("lastEnemyId"));
        battleData.setResetTime(rs.getLong("resetTime"));
        battleData.setNextEnemyUserId(rs.getLong("nextEnemyUserId"));
        battleData.setCaptureEnemyUserId(rs.getLong("captureEnemyUserId"));
        battleData.setCurEnemyUserId(rs.getLong("curEnemyUserId"));
        battleData.setAllBattleTimes(rs.getInt("allBattleTimes"));
        battleData.setBattleType(rs.getInt("battleType"));
        battleData.setBattleEnd(rs.getBoolean("battleEnd"));
        battleData.setAllWin(rs.getBoolean("allWin"));
        battleData.setRandomPatronsList(JSON.parseObject(rs.getString("randomPatronsList"), new TypeReference<List<Integer>>() {}));
        battleData.setBattleList(JSON.parseObject(rs.getString("battleList"), new TypeReference<List<DouLuoDaLuPatronsBattle>>() {}));
        battleData.setEnemyBattleList(JSON.parseObject(rs.getString("enemyBattleList"), new TypeReference<List<DouLuoDaLuPatronsBattleEnemy>>() {}));
        battleData.setGroupId(rs.getInt("groupId"));

        return battleData;
    }
}
